let App = getApp();
Page({
  data: {
    code: '',
    username: '',
    verCode: '',
    imageurl:'',
    password: '',

  },
  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    //刚进入页面随机先获取一个
    this.createCode()

  },
  getPhone: function (e) {
    let _this = this;
    _this.data.username = e.detail.value;

  },
  getName: function (e) {
    let _this = this;
    _this.data.name = e.detail.value;

  },
  getPwd: function (e) {
    let _this = this;
    _this.data.password = e.detail.value;

  },
  getCode: function (e) {
    let _this = this;
    _this.data.verCode = e.detail.value;
  },

  doRegist: function (e) {
    var _this = this;
    var verCode = _this.data.verCode;
    var username = _this.data.username;
    var imageurl = _this.data.imageurl;
    var password = _this.data.password;
    var name = _this.data.name;
    // 简单验证
    if (username.length == 0 || imageurl.length == 0||name.length == 0|| password.length == 0) {
      wx.showToast({
        title: '信息填写不正确',
        icon: 'none',
        duration: 3000
      })
    } else if (_this.data.code.toLowerCase() != verCode.toLowerCase()) {
      wx.showToast({
        title: '验证码不正确',
        icon: 'none',
        duration: 3000
      });


    } else {
      wx.showLoading({
        title: "正在注册",
        mask: true
      });
      // 执行微信登录
      wx.login({
        success: function (res) {
          // 发送用户信息
          App._post_form('user/registForOrg', {
            username: username,
            password: password,
            name: name,
            imageurl:imageurl,
            code: res.code,
            user_info: e.detail.rawData,
            encrypted_data: e.detail.encryptedData,
            iv: e.detail.iv,
            signature: e.detail.signature
          }, function (result) {
            if (result.status == 200) {
              wx.showModal({
                title: '提示',
                content: '注册成功，等待管理员验证',
                showCancel: false,
                success: function (res) {
                  wx.navigateTo({
                    url: '/pages/login/userLogin',
                  })
                }
              });
            } else {
              wx.showModal({
                title: '提示',
                content: '该微信号已绑定用户，请登录',
                showCancel: false,
                success: function (res) {
                  wx.navigateTo({
                    url: '/pages/login/userLogin',
                  })
                }


              });
            }

          }, false, function () {

            wx.hideLoading();
          });
        }
      });
    }
  },



  huanyizhang() {
    this.createCode()
  },
  createCode() {
    var code;
    //首先默认code为空字符串
    code = '';
    //设置长度，这里看需求，我这里设置了4
    var codeLength = 4;
    //设置随机字符
    var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    //循环codeLength 我设置的4就是循环4次
    for (var i = 0; i < codeLength; i++) {
      //设置随机数范围,这设置为0 ~ 36
      var index = Math.floor(Math.random() * 36);
      //字符串拼接 将每次随机的字符 进行拼接
      code += random[index];
    }
    //将拼接好的字符串赋值给展示的code
    this.setData({
      code: code
    })
  },
  
  chooseImage(e) {
    var self = this;

    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        self.setData({
          imageurl: res.tempFilePaths[0]
        })
        wx.uploadFile({
          url: 'https://www.pbsip.cn/api/fileController/add',
          filePath: res.tempFilePaths[0],
          name: 'file',

          success: function (res) {
            console.info("res", res);
            if (res.status == 200)
              self.setData({
                image: res.data
              })
          }
        })
        console.log(res)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

})